package com.len.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.len.controller.req.BillRecordReq;
import com.len.entity.BillRecordEntity;
import com.len.exception.MyException;
import com.len.service.BillRecordService;
import com.len.util.JsonUtil;
import com.len.util.ReType;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "/shop/billRecord")
@Slf4j
@RequiresPermissions("billRecord:showList")
public class BillRecordController {

    @Resource
    private BillRecordService billRecordService;

    @GetMapping(value = "showList")
    public String showList() {
        return "/shop/billRecord/billRecordList";
    }

    @GetMapping(value = "showAdd")
    public String showAdd() {
        return "/shop/billRecord/addBillRecord";
    }

    @GetMapping(value = "getList")
    @ResponseBody
    public String getList(BillRecordReq req, String page, String limit) {

        Page<BillRecordEntity> tPage = new Page(Integer.valueOf(page), Integer.valueOf(limit));
        try {
            tPage = billRecordService.getList(req,tPage);
        } catch (MyException e) {
            e.printStackTrace();
        }
        ReType reType = new ReType(tPage.getTotal(), tPage.getResult());

        return JSON.toJSONString(reType);
    }

    @PostMapping(value = "add")
    @ResponseBody
    public JsonUtil add(BillRecordReq req) throws Exception {
        billRecordService.add(req);
        return JsonUtil.success("操作成功");
    }
}
